//引入axios
import axios from 'axios'
import { ElMessage } from 'element-plus'
import { router } from '@/router/routers.js'
import baseUrl from './baseUrl'
let request = axios.create({
  baseURL: baseUrl,
  timeout: 15000,
})

request.interceptors.request.use(config => {
  if (localStorage.getItem('adminAccessKey')) {
    config.headers['admin-token'] = localStorage.getItem('adminAccessKey')
  }
  return config
}, err => {
  err.response = { code: 500, msg:'请求失败，请检查网络'}
  return err.response
})

request.interceptors.response.use(response => {
  if(response.data.code == -1){
    ElMessage.error('未登录，请登陆后使用')
    return router.push('/login')
  }
  return response.data
}, err => {
  err.response = { code:500, msg:'网络异常，请刷新后重试' }
  return err.response
})

export default request

export function login(formData){
  return request.post('/login',formData)
}

//获取用户列表
export function getUsers(filter){
  return request.get('/user',{params:filter})
}

export function getAdmins(filter){
  return request.get('/member',{params:filter})
}

export function deleteUser(id){
  return request.post(`/user/delete`,{id})
}

export function deleteAdmin(id){
  return request.post(`/member/delete`,{id})
}

export function addAdmin(formData){
  return request.post('/member',formData)
}

export function editAdmin(id,formData){
  return request.put(`/member/${id}`,formData)
}

export function addUser(formData){
  return request.post('/user',formData)
}

export function editUser(id,formData){
  return request.put(`/user/${id}`,formData)
}

export function getAdminLogs(query){
  return request.get('/admin_log',{params:query})
}

export function getPackage(query){
  return request.get('/package',{params:query})
}

export function getNotice(query){
  return request.get('/notice',{params:query})
}

export function deleteNotices(formData){
  return request.post('/notice/delete',formData)
}

export function addNotice(formData){
  return request.post('/notice',formData)
}

export function editNotice(id,formData){
  return request.put(`/notice/${id}`,formData)
}

export function logOut(){
  return request.get('/logout')
}

export function upload(formData){
  return request.post('/upload',formData,{
    'Content-Type': 'multipart/form-data'
  })
}

export function get_customer_service(){
  return request.get('/customer_service')
}

export function edit_customer_service(id,formData){
  return request.post(`/customer_service/${id}`,formData)
}

export function getApiToken(){
  return request.get('/api')
}

export function editApiToken(formData){
  return request.post('/api',formData)
}

export function getApiList(){
  return request.get('/api/list')
}

export function addApi(formData){
  return request.post('/api/save',formData)
}

export function deleteApi(formData){
  return request.post('/api/delete',formData)
}

export function getGive(formData){
  return request.get('/give',{params:formData})
}

export function addGive(formData){
  return request.post('/give',formData)
}

export function deleteGive(formData){
  return request.post('/give/delete',formData)
}

export function updateGive(formData){
  return request.put('/give',formData)
}
